package com.mjfeed.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mjfeed.controller.BaseController;
import com.mjfeed.entity.ShippingMethod;
import com.mjfeed.service.ShippingMethodService;
import com.mjfeed.util.Format;
import com.mjfeed.util.Pagination;
import com.mjfeed.util.ParamBean;

/**
 * @description 配送方式
 * @author zhaominglei
 * @date 2014年10月9日
 */
@Controller("adminShippingMethodController")
@RequestMapping("/admin/shippingMethod")
public class ShippingMethodController extends BaseController {

	@Resource(name="shippingMethodService")
	private ShippingMethodService shippingMethodService;
	
	@RequestMapping(value = "/list", method=RequestMethod.GET)
	public String list(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		paramBean.setUrl(getBasePath(request) + "admin/shippingMethod/list.shtml");
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		Pagination<ShippingMethod> pagination = shippingMethodService.getPagination(paramBean);
		request.setAttribute("pagination", pagination);
		return "admin/shippingMethod/list";
	}
	
	@RequestMapping(value = "/add",method=RequestMethod.GET)
	public String add(HttpServletRequest request, HttpServletResponse response, Model model){
		return "admin/shippingMethod/add";
	}
	
	@RequestMapping(value = "/save", method=RequestMethod.POST)
	public String save(ShippingMethod shippingMethod, HttpServletRequest request, HttpServletResponse response, Model model){
		if (shippingMethod == null) {
			return ERROR_VIEW;
		}
		shippingMethodService.save(shippingMethod);
		return "redirect:/admin/shippingMethod/list.shtml";
	}
	
	@RequestMapping(value = "/edit",method=RequestMethod.GET)
	public String edit(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		if (id == null || id < 1) {
			return ERROR_VIEW;
		}
		request.setAttribute("shippingMethod", shippingMethodService.getById(ShippingMethod.class, id));
		return "admin/shippingMethod/edit";
	}
	
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	public String update(ShippingMethod shippingMethod, HttpServletRequest request, HttpServletResponse response, Model model){
		if (shippingMethod == null || shippingMethod.getId() == null || shippingMethod.getId() < 1) {
			return ERROR_VIEW;
		}
		ShippingMethod pShippingMethod = shippingMethodService.getById(ShippingMethod.class, shippingMethod.getId());
		pShippingMethod.setName(shippingMethod.getName());
		pShippingMethod.setFirstWeight(shippingMethod.getFirstWeight());
		pShippingMethod.setContinueWeight(shippingMethod.getContinueWeight());
		pShippingMethod.setFirstPrice(shippingMethod.getFirstPrice());
		pShippingMethod.setContinuePrice(shippingMethod.getContinuePrice());
		pShippingMethod.setOrders(shippingMethod.getOrders());
		pShippingMethod.setDescription(shippingMethod.getDescription());
		shippingMethodService.update(pShippingMethod);
		return "redirect:/admin/shippingMethod/list.shtml";
	}
	
	@RequestMapping(value = "/view",method=RequestMethod.GET)
	public String view(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		if (id == null || id < 1) {
			return ERROR_VIEW;
		}
		request.setAttribute("shippingMethod", shippingMethodService.getById(ShippingMethod.class, id));
		return "admin/shippingMethod/view";
	}
	
}
